package 锁;
//死锁模拟：两个进程，两把锁
public class Demo_4 {
    static Object A = new Object();
    static Object B = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
           synchronized (A){
               //休眠保证thread1线程可以拿到o2锁
               try {
                   Thread.sleep(500);
               } catch (InterruptedException e) {
                   throw new RuntimeException(e);
               }
               System.out.println("thread线程状态（尝试获取B锁）："+Thread.currentThread().getState());
               synchronized (B){
                   System.out.println("拿到了两把锁");
               }
           }
        });

        Thread t2 = new Thread(()->{
            synchronized (B){
                //休眠保证thread线程可以拿到o1锁
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

                System.out.println("thread1线程状态(尝试获取o1锁)："+Thread.currentThread().getState());
                synchronized (A){
                    System.out.println("拿到了两把锁");
                }
            }
        });

        t1.start();
        t2.start();
    }
}
